package com.dc.project.util.reader;

import com.alibaba.fastjson.JSONObject;
import com.dc.project.util.reader.base.BaseReaderUtil;
import com.dc.project.util.reader.base.FileReaderModel;
import com.volcengine.service.visual.impl.VisualServiceImpl;

import java.io.File;
import java.nio.file.Files;
import java.util.Base64;

/**
 * ImageReaderUtil
 *
 * @author chenxueli
 * @date 2025-03-11 14:30:00
 */
public class ImageReaderUtil implements BaseReaderUtil {

    /**
     * 读取文件内容
     *
     * @param filePath 文件路径
     * @return 文件的文本内容
     */
    @Override
    public FileReaderModel read(String filePath) {
        try {
            var visualService = VisualServiceImpl.getInstance();
            visualService.setAccessKey("AKLTN2UwMGYwMTRkYWE0NGQ4MWFhNzAxYmMwNTEwNWUzNWM");
            visualService.setSecretKey("WW1GaVpHRmpOR1F5TTJVeU5HTTNaRGsyT0Rrek1EWXhNekl5TkRJNFptSQ==");
            // 注意：请根据文档修改相应接口的请求字段名
            JSONObject request = new JSONObject();
            request.put("image_url", filePath);
            String responseText = visualService.ocrApi("OCRNormal", request);
            var response = JSONObject.parseObject(responseText);
            var resultArray = response.getJSONObject("data").getJSONArray("line_texts");
            // 获取
            var builder = new StringBuilder();
            resultArray.forEach(item -> {
                builder.append(item.toString()).append("\n");
            });
            return FileReaderModel.newInstance(builder.toString());
        } catch (Exception e) {
            return FileReaderModel.EMPTY;
        }
    }

    /**
     * 读取文件内容
     *
     * @param file 文件
     * @return 文件内容
     */
    @Override
    public FileReaderModel read(File file) {
        try {
            var fileContent = Files.readAllBytes(file.toPath());
            var base64 = Base64.getEncoder().encodeToString(fileContent);
            var visualService = VisualServiceImpl.getInstance();
            visualService.setAccessKey("AKLTN2UwMGYwMTRkYWE0NGQ4MWFhNzAxYmMwNTEwNWUzNWM");
            visualService.setSecretKey("WW1GaVpHRmpOR1F5TTJVeU5HTTNaRGsyT0Rrek1EWXhNekl5TkRJNFptSQ==");
            // 注意：请根据文档修改相应接口的请求字段名
            JSONObject request = new JSONObject();
            request.put("image_base64", base64);
            String responseText = visualService.ocrApi("OCRNormal", request);
            var response = JSONObject.parseObject(responseText);
            var resultArray = response.getJSONObject("data").getJSONArray("line_texts");
            // 获取
            var builder = new StringBuilder();
            resultArray.forEach(item -> {
                builder.append(item.toString()).append("\n");
            });
            return FileReaderModel.newInstance(builder.toString());
        } catch (Exception e) {
            return FileReaderModel.EMPTY;
        }
    }

    /**
     * 是否可以直接使用url
     *
     * @return true: 可以使用url下载文件并读取内容; false: 需要先下载文件再读取内容
     */
    @Override
    public boolean canUseUrl() {
        return true;
    }

}
